// https://leetcode.cn/problems/permutations/description/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    void dfs(vector<vector<int>>& ans, vector<int>& nums, int index) {
        if (index == nums.size()) {
            ans.push_back(nums);
            return;
        }

        for (int i = index; i < nums.size(); i++) {
            swap(nums[i], nums[index]);
            dfs(ans, nums, index + 1);
            swap(nums[i], nums[index]);
        }
    }

    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> ans;
        dfs(ans, nums, 0);
        return ans;
    }
};